package com.example.communist_app_api.service.impl;

import com.example.communist_app_api.db.dao.UserRoleDao;
import com.example.communist_app_api.db.pojoDto.Role;
import com.example.communist_app_api.exception.GlobalException;
import com.example.communist_app_api.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author love~mlm
 * @version 1.0
 * Create by 2022/11/19 13:01
 */
@Transactional
@Service
public class UserRoleServiceImpl implements UserRoleService {
    @Autowired
    private UserRoleDao userRoleDao;
    @Override
    public ArrayList<Integer>  getRoleIdByUserId(Integer userId) {
        return userRoleDao.getRoleIdByUserId(userId);
    }

    @Override
    public int assignRole(Integer userId, Integer roleId) {
            int deletedNum = userRoleDao.deleteAllUserRole(userId);
        System.out.println("deletedNum = " + deletedNum);
        int insertNum = userRoleDao.assignRole(userId,roleId);

        return insertNum;
    }

    @Override
    public List<Role> getRoleByUserId(Integer userId) {
        return userRoleDao.getRoleByUserId(userId);
    }

    //这个方法仅用于用户由游客变普通党员的状态
    @Override
    public Integer updateUserRole(Integer userId,Integer roleId) {
        return userRoleDao.updateUserRoleByUserId(userId,roleId);
    }

    @Override
    public Boolean deleteUserRoleByUserId(int userId, Integer roleId) {
        return userRoleDao.deleteUserRoleByUserId(userId,roleId);
    }

    @Override
    public Integer insertUserRole(int userId, Integer RoleId) {
        return userRoleDao.assignRole( userId,  RoleId);
    }

    /**
     *
     * @param commissaryInChargeOfPublicityRoleId 宣传委员角色Id
     * @param commissaryInChargeOfOrganizationRoleId 组织委员Id
     */
    @Override
    public void deleteUserRoleByRoleIdUnionUserOrganization(Integer commissaryInChargeOfPublicityRoleId, Integer commissaryInChargeOfOrganizationRoleId,Integer organizationId) {
        Integer commissaryInChargeOfPublicityUserId = userRoleDao.getUserIdByRoleIdAndOrganizationId(commissaryInChargeOfPublicityRoleId,organizationId);
        Integer commissaryInChargeOfOrganizationUserId = userRoleDao.getUserIdByRoleIdAndOrganizationId(commissaryInChargeOfOrganizationRoleId,organizationId);
        Boolean deleteCommissaryInChargeOfPublicity = userRoleDao.deleteUserRoleByUserId(commissaryInChargeOfPublicityUserId, commissaryInChargeOfPublicityRoleId);
        Boolean deleteCommissaryInChargeOfOrganization = userRoleDao.deleteUserRoleByUserId(commissaryInChargeOfOrganizationUserId, commissaryInChargeOfOrganizationRoleId);
        if (!(deleteCommissaryInChargeOfOrganization && deleteCommissaryInChargeOfOrganization)){
            throw new GlobalException("删除失败！");
        }
    }

    @Override
    public void updateUserRoleByUserList(List<Integer> userIdList,Integer VisitorId) {
        userRoleDao.updateUserRoleByUserList(userIdList,VisitorId);
    }
}
